package cn.ejiajunxy.manager.fragment;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.utils.ToastUtils;
import com.leo.baseui.ui.LoadingDataTipsView;
import com.leo.utils.AndroidUtils;

import cn.ejiajunxy.manager.R;
import cn.ejiajunxy.manager.api.ApiService;
import cn.ejiajunxy.manager.common.AppObserver;
import cn.ejiajunxy.manager.model.StatusModel;
import cn.ejiajunxy.manager.utils.UserPrefManager;

/**
 * Created by zhangguobao on 2018/3/17.
 */

public class PriceDialog extends Dialog {
    public float maxPrice;

    public PriceDialog(@NonNull Context context) {
        this(context, R.style.custom_dialog_style);
    }

    public PriceDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView();

        //设置dialog的位置以及大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        lp.width = (int) (AndroidUtils.getDisplayWidth(getContext()) * 0.9);
        dialogWindow.setAttributes(lp);
    }

    private void initView() {
        setContentView(R.layout.dialog_price);
        assignViews();
        init();
    }

    private void init() {
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (TextUtils.isEmpty(etInfo.getText().toString())) {
                        ToastUtils.showToast("支付宝账号不能为空");
                    } else if (TextUtils.isEmpty(etPrice.getText().toString())) {
                        ToastUtils.showToast("金额不能为空");
                    } else if (maxPrice >= 0 && Float.parseFloat(etPrice.getText().toString()) > maxPrice) {
                        ToastUtils.showToast("最大提现金额为" + maxPrice);
                    } else {
                        doRequest();
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void doRequest() {
        loading.showLoadingView();
        RxHttpUtils.createApi(ApiService.class)
                .addCash(UserPrefManager.getToken(getContext()), etInfo.getText().toString(), etPrice.getText().toString())
                .compose(Transformer.<StatusModel>switchSchedulers())
                .subscribe(new AppObserver<StatusModel>() {
                    @Override
                    protected void success(StatusModel statusModel) {
                        loading.hideLoadingView();
                        if (statusModel == null || statusModel.status != 0) {
                            ToastUtils.showToast("提现失败");
                            return;
                        }
                        ToastUtils.showToast("提现成功");
                        dismiss();
                    }

                    @Override
                    protected void onError(String s) {
                        loading.hideLoadingView();
                        ToastUtils.showToast("网络失败");
                    }
                });
    }

    private EditText etInfo;
    private EditText etPrice;
    private TextView tvConfirm;
    private LoadingDataTipsView loading;

    private void assignViews() {
        etInfo = (EditText) findViewById(R.id.etInfo);
        etPrice = findViewById(R.id.etPrice);
        tvConfirm = findViewById(R.id.tvConfirm);
        loading = findViewById(R.id.loading);
        loading.hideLoadingView();
    }


}
